﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Work.ClientEngine.FPXH
{
    internal class AddressRformat : CommandFormat
    {
        public AddressRformat(string addresscode) : base(addresscode)
        {
        }

        public override EventArgs AnalysisReadPacket(string cmd, int count, int slaveID = 1)
        {
            ReadDataEventArgs args = new ReadDataEventArgs()
            {
                DebugMsg = string.Empty,
                Value = new int[count]
            };
            if (cmd.Length > 9)
            {
                //读取到数据
                int length = 9 + count;
                if (cmd.Length != length)
                    args.DebugMsg = "数据丢包";
                string ds = cmd.Substring(6, count);
                for (int i = 0; i < count; i++)
                {
                    args.Value[i] = int.Parse(ds[i].ToString());
                }
            }
            else if (cmd.Length == 9)
            {
                args.DebugMsg = cmd.Substring(4, 2);
            }
            else
            {
                args.DebugMsg = "数据丢包";
            }
            return args;
        }

        public override EventArgs AnalysisWritePacket(string cmd, int slaveID = 1)
        {
            WriteDataEventArgs args = new WriteDataEventArgs()
            {
                WriteOK = true,
                DebugMsg = string.Empty
            };
            if (cmd.Length == 9 && cmd.StartsWith("%01$WC"))
            {

            }
            else if (cmd.Length == 9)
            {
                args.WriteOK = false;
                args.DebugMsg = cmd.Substring(4, 2);
            }
            else
            {
                args.WriteOK = false;
                args.DebugMsg = "命令数据丢包";
            }

            return args;
        }

        public override byte[] MakeReadPacket(string[] address, int[] values, int slaveID = 1)
        {
            int count = values[0];
            string cmd = null;
            if (count == 1)
            {
                cmd = string.Format("%{0}#RCS{1}", slaveID.ToString("00"), address[0]);
            }
            else
            {
                cmd = string.Format("%{0}#RCP{1}", slaveID.ToString("00"), count);
                for (int i = 0; i < count; i++)
                    cmd += address[i];
            }
            cmd = string.Format("{0}{1}{2}", cmd, GetBCC(cmd), "\r");
            return Encoding.ASCII.GetBytes(cmd);
        }

        public override byte[] MakeWritePacket(string[] address, int[] values, int slaveID = 1)
        {
            int count = values.Length;
            string cmd = null;
            if (count == 1)
            {
                cmd = string.Format("%{0}#WCS{1}{2}", slaveID.ToString("00"), address[0], values[0]);
            }
            else
            {
                cmd = string.Format("%{0}#WCP{1}", slaveID.ToString("00"), count);
                for (int i = 0; i < count; i++)
                    cmd += string.Format("{0}{1}", address[i], values[i]);
            }
            cmd = string.Format("{0}{1}{2}", cmd, GetBCC(cmd), "\r");
            return Encoding.ASCII.GetBytes(cmd);
        }
    }
}
